<template>
    <el-card class="box-card" width="100%">
  <div slot="header" class="clearfix">
    <span>数据集创建</span>
    <el-button style="float: right; padding: 1px 0; height: 35px; width: 55px" type="primary" @click="goBack()">返回</el-button>
  </div>
  <div class="forms">
    <el-form ref="detailData" :rules="rules" :model="detailData" label-width="100px" class="dataset-form">
      <el-form-item label="数据集名称：" prop="name">
        <el-input v-model="detailData.name"></el-input>
      </el-form-item>
      <el-form-item label="是否公有：" prop="public">
        <el-switch
          v-model="detailData.public"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="是"
          inactive-text="否"
          :active-value=1
          :inactive-value=0>
        </el-switch>
      </el-form-item>
      <el-form-item label="数据集文件：" prop="path">
          <el-upload
      class="upload-demo"
      action="string"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :limit="1"
      :on-change="handleChange"
      :on-exceed="handleExceed"
      :on-success="handleSuccess"
      :file-list="fileList"
      :auto-upload="false">
      <el-button size="small" type="primary">选择文件</el-button>
      <div slot="tip" class="el-upload__tip">上传数据集文件</div>
      </el-upload>
        <!-- <el-input v-model="detailData.path"></el-input> -->
      </el-form-item>
      <el-form-item label="数据集描述：" prop="description">
        <el-input v-model="detailData.description" type="textarea" :rows="5" maxlength=500></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="createDatasetProcess()">提交</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据集id: {{detailData.id}} -->
  </div>
  <!-- <div class="text item">
    数据集名称： {{detailData.name}}
  </div>
  <div class="text item">
    创建者： {{detailData.user_id}}
  </div>
  <div class="text item">
    创建时间： {{detailData.create_date}}
  </div>
  <div class="text item">
    是否公有： {{detailData.public}}
  </div>
  <div class="text item">
    描述： {{detailData.description}}
  </div> -->
</el-card>
</template>
<style scoped>
  .forms {
    font-size: 16px;
    text-align: left;
    margin-left: 1px;
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 100%;
  }

  .el-card__header {
    padding: 18px 20px;
    background-color: rgb(238, 241, 246);
  }

  .dataset-form {
    width: 90%;
    margin: 10px; /* 上下间距160px，左右自动居中*/
    background-color: rgb(255, 255, 255, 0.8); /* 透明背景色 */
    padding: 10px;
    border-radius: 20px; /* 圆角 */
  }
</style>
<script>
import { createDatasetByFile } from '@/api/request/dataset'
import { upload, status } from '@/api/request/upload'
import { Message } from 'element-ui'
// import store from '@/store/store'
export default {
//   props: ['title'],
  data () {
    return {
      detailData: {
        public: 0,
        description: '',
        name: '',
        path: '',
        filebytes: ''
      },
      fileList: []
    //   title: '我创建的数据集'
    }
  },
  methods: {
    update () {
    //   getDataset(store.datasetInfo.curID).then(response => {
    //     this.detailData = response
    //   })
    //   this.detailData = null
    },
    goBack () {
      this.clear()
      this.$router.go(-1)
    },
    async createDatasetProcess () {
      let uploadRes = await this.uploadFile(this.fileList[0].raw)
      if (uploadRes) {
        createDatasetByFile({dataset_name: this.detailData.name, description: this.detailData.description, public: this.detailData.public, file_name: uploadRes}).then(response => {
          this.clear()
          this.$router.go(-1)
        })
      } else {
        Message({
          message: 'Upload File Failed',
          type: 'error',
          duration: 3 * 1000
        })
      }
      // let formData = new FormData()
      // formData.append('dataset_name', this.detailData.name)
      // formData.append('description', this.detailData.description)
      // formData.append('public', this.detailData.public)
      // formData.append('filebytes', this.fileList[0].raw, 'filebytes')
      // createDataset(formData).then(response => {
      //   this.clear()
      //   this.$router.go(-1)
      // })
    },
    handleSuccess (response, file, fileList) {
      let reader = new FileReader()
      reader.readAsBinaryString(file.raw)
      reader.onload = e => {
        this.detailData.filebytes = e.target.result
      }
    },
    handleChange (file, fileList) {
      this.fileList = fileList
    },
    clear () {
      this.detailData.name = ''
      this.detailData.filebytes = null
      this.detailData.description = ''
    },
    async uploadFile (fileRaw) {
      console.log(fileRaw)
      var statusRes = await status()
      console.log(statusRes)
      // return
      if (statusRes.status !== 200) {
        Message({
          message: statusRes.data,
          type: 'error',
          duration: 3 * 1000
        })
        return null
      }
      if (statusRes.data.success === 0) {
        let fileName = statusRes.data.data.file_name
        let fileSize = statusRes.data.data.file_size
        while (fileSize < fileRaw.size) {
          let formData = new FormData()
          formData.append('file_size', fileSize)
          formData.append('file', fileRaw.slice(fileSize, Math.min(fileSize + 1024 * 1024, fileRaw.size)), fileName)
          let uploadRes = await upload(formData)
          console.log(uploadRes)
          if ((uploadRes.status !== 200) || (uploadRes.data.success !== 0)) {
            statusRes = await status({file_name: fileName})
            fileName = statusRes.data.data.file_name
            fileSize = uploadRes.data.data.file_size
            continue
          }
          // statusRes = await status({file_name: fileName})
          // console.log(statusRes)
          // if ((status_res.status !== 200) || {
          // fileName = statusRes.data.data.file_name
          fileSize = uploadRes.data.data.file_size
        }
        return fileName
      }
      return null
    }
  },
  //   created () {
  //     console.log('created')
  //     this.update()
  //   },
  activated () {
    // if (store.datasetInfo.list_type === this.list_type) {
    //   console.log('==')
    //   return
    // }
    this.update()
  }
}
</script>
